﻿
$.CFormat = {
    Culture: function () {
        return "vi-VN";
    },
    ParseFloat: function (value, culture) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            ret = $.parseFloat(value, "n", culture);
        }
        return ret;
    },
    ParseDate: function (value, culture) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            var temp = value.split(" ");
            if (temp && temp.length > 0) {
                var date = temp[0];
                var time = temp[1] || "";
                var hour = 0, minute = 0, second = 0;
                if (time) {
                    var arrTime = time.split(":");
                    if (arrTime && arrTime.length > 0) {
                        hour = arrTime[0] || 0;
                        minute = arrTime[1] || 0;
                        second = arrTime[2] || 0;
                    }
                }
                var arrDate = date.split("/");
                if (date.indexOf("-") != -1)
                    arrDate = date.split("-");
                var year, month, day;

                if (arrDate && arrDate.length > 0) {
                    if (arrDate[0].length == 4) {
                        year = arrDate[0];
                        month = arrDate[1] - 1;
                        day = arrDate[2];
                    }
                    else {
                        if (date) {
                            var cdate = $.parseDate(date, "d", culture);
                            if (cdate) {
                                year = cdate.getFullYear();
                                month = cdate.getMonth();
                                day = cdate.getDate();
                            }
                        }
                        
                    }
                }
                ret = new Date(year, month, day, hour, minute, second, 0);
            }
        }
        return ret;
    },
    Format: function (value, format, culture) {
        var ret = "";
        culture = culture || thisObj.Culture();
        ret = $.format(value, format, culture);
        return ret;
    },
    FormatNumber: function (value, format, culture, cultureTo) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            format = format || "n";
            if (typeof value != "number")
                value = thisObj.ParseFloat(value, culture);
            cultureTo = cultureTo || thisObj.Culture();
            ret = thisObj.Format(value, format, cultureTo);
        }
        return ret;
    },
    FormatDate: function (value, format, culture, cultureTo) {
        var thisObj = this;
        culture = culture || "en-US";
        var ret = "";
        if (value != "") {
            format = format || "d";
            if (typeof value == "string")
                value = thisObj.ParseDate(value, culture);
            cultureTo = cultureTo || thisObj.Culture();
            ret = thisObj.Format(value, format, cultureTo);

        }
        return ret;
    },
    FormatDateSerializer: function (value) {
        if (value && value.toString().indexOf("/Date") != -1) {
            value = new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
            value = $.CFormat.FormatDate(value, "d", "vi-VN");
        }
        return value;
    }
};